$(document).ajaxSend(function (e, xhr, opt) {
  var user_token = localStorage.getItem("user_token");
  xhr.setRequestHeader("user_token_header", user_token);
});
//放在common.js中，这时候所有引入common.js的页面都会执行这个代码。

$(document).ajaxError(function (event, jqxhr, settings, thrownError) {
  // 检查是否是未授权错误
  if (jqxhr.status === 401) {
    alert("登录已失效，请重新登录！");
    window.location.href = "/blog_login.html";
  } else {
    // 处理其他错误
    console.error("AJAX 请求出错，状态码:", jqxhr.status);
    alert("请求出错，请稍后再试！");
  }
});

function getUserInfo(url) {
  $.ajax({
    type: "get",
    url: url,
    success: function (result) {
      if (result.code == 200 && result.data != null) {
        $(".left .card h3").text(result.data.userName);
        $(".left .card a").attr("href", result.data.githubUrl);
      }
    },
  });
}

function getCountsBlog (url) {
  $.ajax({
    type: "get",
    url: url,
    success: function (result) {
      if (result.code == 200 && result.data != null) {
        // 获取元素
        var articleCountSpan = document.getElementById("articleCount");
        console.log(result.data);
        console.log(articleCountSpan);

        if (articleCountSpan) {
        articleCountSpan.innerHTML = result.data;
        } else {
            console.log('没能正确获取到目标元素');
        }
        // 设置新的值，这里假设新值为10，你可以根据实际需求替换成相应的值
        
      }
    },
  });
}

function logout() {
  localStorage.removeItem("user_token");
  location.href = "blog_login.html";
}
